package dk.isten.andro.main;  


import java.io.InputStream;
import java.util.ArrayList;

import junit.framework.Assert;
import android.content.Context;
import android.content.Intent;
import android.gesture.GestureLibrary;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import dk.isten.andro.R;


public class Truppen_Akt extends SuperActivity implements OnItemClickListener, OnClickListener{

	static ArrayList<util.Item> spillere = new ArrayList<util.Item>();
	ImageView roed, horn,gul, imageView1;
	String sponsor;
	TextView startopstilling;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trup_holder);

		gul = (ImageView) findViewById(R.id.yellow);
        horn = (ImageView) findViewById(R.id.horn);
        roed = (ImageView) findViewById(R.id.red);
        imageView1 = (ImageView) findViewById(R.id.imageView1);
//        startopstilling = (TextView) findViewById(R.id.startopstilling);
		
		imageView1.setOnClickListener(this);
        gul.setOnClickListener(this);
        horn.setOnClickListener(this);
        roed.setOnClickListener(this);
//        startopstilling.setOnClickListener(this);
        
       
		Parse();

			//sæt listview til det rigtig layout.
		ListView listView2 = (ListView) findViewById(R.id.ListView02);
		listView2.setOnItemClickListener(this);
		
		//sæt indholdet af listview.
		listView2.setAdapter(new ArrayAdapter(this, R.layout.listeelement_trup, R.id.listeelem_overskrift, spillere) {
			
			@Override
			public View getView(int position, View cachedView, ViewGroup parent) {
				View view = super.getView(position, cachedView, parent);
				
				if(!spillere.get(position).getSponsor().equals("0")){
					sponsor = "Sponseret af: " + spillere.get(position).getSponsor();
					Log.d("sponsor felt", spillere.get(position).getSponsor() );
				}else{
					sponsor = spillere.get(position).getPosition();
				}
				
				TextView listeelem_overskrift = (TextView) view.findViewById(R.id.listeelem_overskrift); 	//sæt layout for TextView
				listeelem_overskrift.setText( spillere.get(position).Troejenummer + " " + spillere.get(position).getNavn()); 			//SetText fra ArrayList
				TextView listeelem_beskrivelse = (TextView) view.findViewById(R.id.listeelem_beskrivelse);
				listeelem_beskrivelse.setText(sponsor); 
				ImageView listeelem_billede = (ImageView) view.findViewById(R.id.listeelem_billede); 
				listeelem_billede.setImageResource((getDrawable(Truppen_Akt.this, spillere.get(position).getImage())));

				
				return view;
			}
		});

	}
	
	public void onClick(View v) {

		if(v==imageView1){
            
        	Intent i = new Intent(this, FrontActivity.class);
           
                    startActivity(i);
                    
        }else if(v==gul){
                
                	Intent i = new Intent(this, yellow.class);
                   
                            startActivity(i);
                            
                }else if(v==roed){
                	Intent i = new Intent(this, red.class);
                    
                    		startActivity(i);
                }else if(v==horn){
                	
                	MediaPlayer enLyd = MediaPlayer.create(this, R.raw.vuvuzela);
                    enLyd.setVolume(9, 9);
                    enLyd.start();
        } 
//                else if (v==startopstilling){
//        	Intent i = new Intent(this, Startopstilling_Akt.class);
//            
//    		startActivity(i);
//        }
                }
	
	
	
//start en ny activity med en enkelt spiller
	public void onItemClick(AdapterView<?> l, View v, int position, long id) {

		Intent i = new Intent(this, SpillerProfil_Akt.class);
//		send information om billedIdentifier med intent
		i.putExtra("position", position);
		startActivity(i);
	}
public void Parse() {

		try {
	
			InputStream is = getResources().openRawResource(R.raw.truppen);
			byte b[] = new byte[is.available()]; // kun smÃ¥ filer
			is.read(b);
			String str = new String(b, "ISO-8859-1"); //UTF-8 virker ikke

			
			for (String linje : str.split("\n")) {
				util.Item spiller = new util.Item();
				
				linje = linje.trim();
				if (linje.length() == 0 || linje.startsWith("#")) continue;
				String[] felter = linje.split(",");
				spiller.image= felter[0];
				spiller.navn = felter[1];
				spiller.sponsor = felter[2];
				spiller.Troejenummer = felter[3];
				spiller.position = felter[4];
				spillere.add(spiller);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("FEJL:" + ex.toString());
		}

	}

//find Identifier for et billede i drawable ud fra navnet
public static int getDrawable(Context context, String name)
{
    Assert.assertNotNull(context);
    Assert.assertNotNull(name);

    return context.getResources().getIdentifier(name,
            "drawable", context.getPackageName());
}
}
